package ysq.xyg.host.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.host.dao.HostPriceSearchDao;
import ysq.xyg.host.model.dto.HostPriceSearchDto;
import ysq.xyg.host.model.po.HostPriceSearch;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.host.model.vo.HostPriceSearchVo;
import ysq.xyg.host.service.HostPriceSearchService;

import java.util.Date;
import java.util.List;

/**
 * 房产-价格搜查表service实现
 *
 * @author congcong tao
 * @date 2019-12-11 15:06:27
 */
@Service("hostPriceSearchService")
public class HostPriceSearchServiceImpl extends BaseServiceImpl<HostPriceSearch> implements HostPriceSearchService {

    @Autowired
    private HostPriceSearchDao hostPriceSearchDao;

    @Autowired
    public void setBaseDao(HostPriceSearchDao hostPriceSearchDao) {
        this.baseDao = hostPriceSearchDao;
    }

    /**
     * 分页查询
     *
     * @param hostPriceSearchVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:06:27
     */
    @Override
    public ReturnEntity page(HostPriceSearchVo hostPriceSearchVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" order_num asc, create_time desc");
        List<HostPriceSearch> list = acceptList(hostPriceSearchVo);
        PageInfo<HostPriceSearch> page = new PageInfo<HostPriceSearch>(list);
        return new ReturnEntity(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param hostPriceSearchVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:06:27
     */
    @Override
    @CacheEvict(value={"cacheHostPriceSearchByCityIdList"}, allEntries=true)
    public ReturnEntity addOrUpdate(HostPriceSearchVo hostPriceSearchVo, SysUser currentUser) {
        boolean isUpate = true;
        HostPriceSearch hostPriceSearch;
        if (StrUtil.isEmpty(hostPriceSearchVo.getId())) {
            hostPriceSearch = new HostPriceSearch(currentUser);
            isUpate = false;
        } else {
            hostPriceSearch = selectByPrimaryKey(hostPriceSearchVo.getId());
            if (ObjectUtil.isNull(hostPriceSearch)) {
                return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
            }
            hostPriceSearch.setUpdateTime(new Date());
            hostPriceSearch.setUpdateUser(currentUser.getId());
        }
        hostPriceSearch.setCityId(hostPriceSearchVo.getCityId());
        hostPriceSearch.setName(hostPriceSearchVo.getName());
        hostPriceSearch.setMinPrice(hostPriceSearchVo.getMinPrice());
        hostPriceSearch.setMaxPrice(hostPriceSearchVo.getMaxPrice());
        hostPriceSearch.setOrderNum(hostPriceSearchVo.getOrderNum());
        int num = 0;
        if (isUpate) {
            num = updateByPrimaryKey(hostPriceSearch);
        } else {
            num = insert(hostPriceSearch);
        }
        if (num <= 0) {
            return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:06:27
     */
    @Override
    @CacheEvict(value={"cacheHostPriceSearchByCityIdList"}, allEntries=true)
    public ReturnEntity delete(String id, SysUser currentUser) {
        HostPriceSearch hostPriceSearch = selectByPrimaryKey(id);
        if (hostPriceSearch != null) {
            hostPriceSearch.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            hostPriceSearch.setUpdateTime(new Date());
            hostPriceSearch.setUpdateUser(currentUser.getId());
            int num = updateByPrimaryKey(hostPriceSearch);
            if (num > 0) {
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param hostPriceSearchVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-12-11 15:06:27
     */
    @Override
    public List<HostPriceSearch> acceptList(HostPriceSearchVo hostPriceSearchVo){
        Example example = new Example(HostPriceSearch.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(hostPriceSearchVo.getRemoveTag())) {
            criteria.andEqualTo("removeTag", hostPriceSearchVo.getRemoveTag());
        }
        if (StringUtil.isNotEmpty(hostPriceSearchVo.getAccountId())) {
            criteria.andEqualTo("accountId", hostPriceSearchVo.getAccountId());
        }
        if (StringUtil.isNotEmpty(hostPriceSearchVo.getCityId())) {
            criteria.andEqualTo("cityId", hostPriceSearchVo.getCityId());
        }
        example.setOrderByClause(" order_num asc, update_time desc");
        return hostPriceSearchDao.selectByExample(example);
    }

    @Override
    public List<HostPriceSearchDto> selectHostPriceSearchByCityIdList(String cityId) {
        return hostPriceSearchDao.selectHostPriceSearchByCityIdList(cityId);
    }
}
